﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;

namespace RedRiver.Framework.XtraCommon.UserAuthentication{
    /// <summary>
    /// 用户上下文信息
    /// </summary>
    public static class UserContext{
        /// <summary>
        /// 内部字典
        /// </summary>
        private static readonly Dictionary<string, object> InnerDictionary = new Dictionary<string, object>();

        private static BaseInfo _defaultGlobal;

        /// <summary>
        /// 全局参数
        /// </summary>
        public static BaseInfo GlobalConst{
            get { return _defaultGlobal ?? (_defaultGlobal = new BaseInfo()); }
        }

        #region Nested type: BaseInfo

        /// <summary>
        /// 全局常量
        /// </summary>
        public class BaseInfo : IEditableObject{
            /// <summary>
            /// 所在车间
            /// </summary>
            [DisplayName(@"所在车间")]
            public string Workshop{
                get{
                    if (!InnerDictionary.ContainsKey(ConstKey.KeyWorkshop)){
                        return string.Empty;
                    }
                    return InnerDictionary[ConstKey.KeyWorkshop].ToString();
                }
                set { SetProperty(ConstKey.KeyWorkshop, value); }
            }

            /// <summary>
            /// 应用系统名称
            /// </summary>
            [DisplayName(@"应用系统名称")]
            public string ApplicationName{
                get{
                    if (!InnerDictionary.ContainsKey(ConstKey.KeyApplicationName)){
                        return Application.ProductName;
                    }
                    return InnerDictionary[ConstKey.KeyApplicationName].ToString();
                }
                set { SetProperty(ConstKey.KeyApplicationName, value); }
            }

            /// <summary>
            /// 用户名
            /// </summary>
            [DisplayName(@"登录名")]
            public string UserName{
                get{
                    if (!InnerDictionary.ContainsKey(ConstKey.KeyUserName)){
                        return Thread.CurrentPrincipal.Identity.Name;
                    }
                    return InnerDictionary[ConstKey.KeyUserName].ToString();
                }
                set { SetProperty(ConstKey.KeyUserName, value); }
            }

            /// <summary>
            /// 当前用户工号
            /// </summary>
            [DisplayName(@"工号")]
            public string EmployeeCode{
                get{
                    if (!InnerDictionary.ContainsKey(ConstKey.KeyEmplyeeCode)){
                        return Thread.CurrentPrincipal.Identity.Name;
                    }
                    return InnerDictionary[ConstKey.KeyEmplyeeCode].ToString();
                }
                set { SetProperty(ConstKey.KeyEmplyeeCode, value); }
            }

            /// <summary>
            /// 当前用户姓名全称
            /// </summary>
            [DisplayName(@"用户姓名")]
            public string UserFullName{
                get{
                    if (!InnerDictionary.ContainsKey(ConstKey.KeyFullName)){
                        return Thread.CurrentPrincipal.Identity.Name;
                    }
                    return InnerDictionary[ConstKey.KeyFullName].ToString();
                }
                set { SetProperty(ConstKey.KeyFullName, value); }
            }

            /// <summary>
            /// 操作日期
            /// </summary>
            [DisplayName(@"操作日期")]
            public DateTime CurrentDate{
                get{
                    if (!InnerDictionary.ContainsKey(ConstKey.KeyCurrentDate)){
                        return DateTime.Today;
                    }
                    return (DateTime) InnerDictionary[ConstKey.KeyCurrentDate];
                }

                set { SetProperty(ConstKey.KeyCurrentDate, value); }
            }

            /// <summary>
            /// 远程服务基础Url
            /// </summary>
            [DisplayName(@"远程服务基础Url")]
            public string BaseUrl{
                get{
                    if (!InnerDictionary.ContainsKey(ConstKey.KeyBaseURL)){
                        return string.Empty;
                    }
                    return InnerDictionary[ConstKey.KeyBaseURL].ToString();
                }
                set { SetProperty(ConstKey.KeyBaseURL, value); }
            }

            //public const string DefaultValueNoSetDepCode = "*未设置部门代码*";
            /// <summary>
            /// 用户所在部门代码
            /// </summary>
            [DisplayName(@"部门代码")]
            public string UserDepCode{
                get{
                    return !InnerDictionary.ContainsKey(ConstKey.KeyUserDepCode) ? string.Empty : InnerDictionary[ConstKey.KeyUserDepCode].ToString();
                }
                set { SetProperty(ConstKey.KeyUserDepCode, value); }
            }

            /// <summary>
            /// 用户所在部门名称
            /// </summary>
            [DisplayName(@"部门名称")]
            public string UserDepName{
                get{
                    if (!InnerDictionary.ContainsKey(ConstKey.KeyUserDepName)){
                        return string.Empty; //"*未设置部门*";
                    }
                    return InnerDictionary[ConstKey.KeyUserDepName].ToString();
                }
                set { SetProperty(ConstKey.KeyUserDepName, value); }
            }

            public object this[string key]{
                get { return InnerDictionary[key]; }

                set { SetProperty(key, value); }
            }

            private void SetProperty(string propertyName, object value){
                if (InnerDictionary.ContainsKey(propertyName)){
                    if (!InnerDictionary[propertyName].Equals(value)){
                        InnerDictionary[propertyName] = value;
                        OnPropertyChanged(new PropertyChangingEventArgs(propertyName));
                    }
                } else{
                    InnerDictionary.Add(propertyName, value);
                    OnPropertyChanged(new PropertyChangingEventArgs(propertyName));
                }
            }

            /// <summary>
            /// 属性值变更
            /// </summary>
            public event EventHandler<PropertyChangingEventArgs> PropertyChanged;

            protected void OnPropertyChanged(PropertyChangingEventArgs e){
                EventHandler<PropertyChangingEventArgs> handler = PropertyChanged;
                if (handler != null){
                    handler(this, e);
                }
            }

            public event EventHandler ObjectChanged;

            private void OnObjectChanged(EventArgs e){
                EventHandler handler = ObjectChanged;
                if (handler != null){
                    handler(this, e);
                }
            }

            #region Implementation of IEditableObject

            public void BeginEdit() {}

            public void EndEdit(){
                OnObjectChanged(EventArgs.Empty);
            }

            public void CancelEdit(){
                throw new NotImplementedException();
            }

            #endregion
        }

        #endregion

        #region Nested type: ConstKey

        public static class ConstKey{
            public const string KeyApplicationName = "ApplicationName";

            /// <summary>
            /// 远程连接基础路径
            /// </summary>
            public const string KeyBaseURL = "BaseUrl";

            /// <summary>
            /// 当前操作日期
            /// </summary>
            public const string KeyCurrentDate = "CurrentDate";

            ///<summary>
            /// 用户工号
            /// </summary>
            public const string KeyEmplyeeCode = "EmplyeeCode";

            /// <summary>
            /// 当前用户姓名全称
            /// </summary>
            public const string KeyFullName = "FullName";

            /// <summary>
            /// 工序ID
            /// </summary>
            public const string KeyProcId = "ProcId";

            ///<summary>
            /// 当前用户
            /// </summary>
            public const string KeyUserDepCode = "UserDepCode";

            /// <summary>
            /// 部门名称
            /// </summary>
            public const string KeyUserDepName = "UserDepName";

            /// <summary>
            /// 当前用户登录名
            /// </summary>
            public const string KeyUserName = "UserName";

            public const string KeyWorkLine = "WorkLine";
            public const string KeyWorkshop = "Workshop";
            public const string KeyWorkStation = "WorkStation";
            public const string KeyWorkTeam = "Team";
        }

        #endregion

    }
}